package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.NewFriendAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.GetNewFriendListTask;
import com.xgkj.diyiketang.task.ManagerFriendTask;
import com.xgkj.diyiketang.task.StudentListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.MyLoadDataListView;

import java.util.ArrayList;
import java.util.List;

public class NewFriendActivity extends BaseActivity implements MyLoadDataListView.LoadDataListener,NewFriendAdapter.OnAdapterClickListener{
    private Context mContext;
    private MyLoadDataListView mListView;
    private NoDataUtil noDataUtil;
    private NewFriendAdapter mAdapter;
    private int pageNo;
    private int size = 20;
    private int newfriendtaskId = -1;
    private int managerTaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_friend);
        mContext = this;
        initView();
    }

    private void initView() {
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListView = (MyLoadDataListView) findViewById(R.id.newfriend_list);
        mAdapter = new NewFriendAdapter(mContext);
        mAdapter.setOnAdapterClickListener(this);
        mListView.setAdapter(mAdapter);
        View nodataView = findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext,nodataView);
        mListView.setOnLoadDataListener(this);
        pageNo = 1;
        getNewFriendList();
    }

    private void getNewFriendList(){
        if (pageNo == 0){
            mListView.setNoNextPage();
            return;
        }
        StudentListTask task = new StudentListTask(size,pageNo,2);
        task.addListener(mListener);
        newfriendtaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == newfriendtaskId ){
                if (data != null){
                    List<EatShowUserInfo> list = new ArrayList<EatShowUserInfo>();
                    list = (List<EatShowUserInfo>) data;
                    if (list != null && list.size()>0){
                        if (pageNo == 1){
                            mAdapter.clearData();
                            mListView.enableLoadMore();
                        }
                        mAdapter.updateData(list);
                    }

                    if (mAdapter != null && mAdapter.getCount()>0){
                        noDataUtil.ListViewNoEmpty(mListView);
                    }else {
                        noDataUtil.ListViewEmpty(mListView,null,null,-1);
                    }

                    pageNo = result;
                }
            }else if (taskId == managerTaskId){
                pageNo = 1;
                getNewFriendList();
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == newfriendtaskId){
                mListView.setFootProgressVisible(false);
            }
        }
    };

    @Override
    public void onLoadMore() {
        getNewFriendList();
    }

    @Override
    public void onReceiveClick(String userId) {
        doReceiveFriend(userId);
    }

    private void doReceiveFriend(String userid){
        ManagerFriendTask task = new ManagerFriendTask(userid,"1");
        task.addListener(mListener);
        managerTaskId = getEatShowApp().getTaskManager().addTask(task);
    }
}
